﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using DTO;


namespace DAO
{
    public class THANHPHO_DAO
    {
        #region 1. Inserting
        public static bool ThemThanhPho(THANHPHO_DTO tp)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTP", tp.MaTP));
                sqlParams.Add(new SqlParameter("@TenTP", tp.TenTP));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("InsertThanhPho", sqlParams);

                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 2. Updating
        public static bool SuaThanhPho(THANHPHO_DTO tp)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTP", tp.MaTP));
                sqlParams.Add(new SqlParameter("@TenTP", tp.TenTP));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("UpdateThanhPho", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 3. Deleting
        public static bool XoaThanhPho(int maTP)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTP", maTP));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("DeleteThanhPho", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion


        public static List<THANHPHO_DTO> SelectThanhPhosAll()
        {
            List<THANHPHO_DTO> list = new List<THANHPHO_DTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("SelectThanhPhosAll");
                foreach (DataRow dr in dt.Rows)
                {
                    THANHPHO_DTO tp = new THANHPHO_DTO();
                    tp.MaTP = (int)dr["MaTP"];
                    tp.TenTP = dr["TenTP"].ToString();
                    list.Add(tp);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static THANHPHO_DTO SelectThanhPhosByAndMaTP(int maTP)
        {
            THANHPHO_DTO tp = new THANHPHO_DTO();
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTP", maTP));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("SelectThanhPhosByAndMaTP", sqlParams);
                DataRow dr = dt.Rows[0];
                tp.MaTP = (int)dr["MaTP"];
                tp.TenTP = dr["TenTP"].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return tp;
        }


        public static bool CheckThanhPhoByMaTP(int maTP)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTP", maTP));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("SelectThanhPhosByAndMaTP", sqlParams);
                if (dt.Rows.Count == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static int SelectThanhPhoByTen(String tenTP)
        {
            int n;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@TenTP", tenTP));
                n = SqlDataAccessHelper.ExecuteScalar("SelectThanhPhoByTen", sqlParams);
            }

            catch (Exception ex)
            {
                throw ex;
            }
            return n;
        }
    }
}


